//
// Created by king on 18-11-14.
//

#include "common.h"
#include "gl_util.h"

#include <android/bitmap.h>

auto gVertexShader =
        "attribute vec4 aPosition;\n"
        "attribute vec2 a_TexCoord;\n"
        "varying vec2 v_TexCoord;\n"
        "void main() {\n"
        "  gl_Position = aPosition;\n"
        "  v_TexCoord = a_TexCoord;\n"
        "}\n";

auto gFragmentShader =
        "precision mediump float;\n"
        "uniform sampler2D u_Sampler;\n"
        "varying vec2 v_TexCoord;\n"
        "void main() {\n"
        "  gl_FragColor = texture2D(u_Sampler, v_TexCoord);\n"
        "}\n";


const GLfloat gTriangleVertices[] = {
        -0.5f, 0.5f,  0.0f, 1 - 1.0f,
        -0.5f, -0.5f, 0.0f, 1 - 0.0f,
        0.5f,  0.5f,  1.0f, 1 - 1.0f,
        0.5f,  -0.5f, 1.0f, 1 - 0.0f};


GLuint gProgram;
GLuint gaPositionHandle;
GLuint gaTexCoordHandle;
GLuint uTextureSamplerHandle;

extern "C" JNIEXPORT void JNICALL
Java_com_example_king_openglenvdemo_nativegl_GLES2JNILib_init(
        JNIEnv* env,
        jobject obj,
        jint width,
        jint height) {

    glViewport(0, 0, width, height);

    gProgram = linkProgram(gVertexShader, gFragmentShader);
    if (!gProgram) {
        LOGE("Could not create program.");
        return;
    }

    glUseProgram(gProgram);

    gaPositionHandle = glGetAttribLocation(gProgram, "aPosition");
    glVertexAttribPointer(gaPositionHandle, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), gTriangleVertices);
    glEnableVertexAttribArray(gaPositionHandle);

    gaTexCoordHandle = glGetAttribLocation(gProgram, "a_TexCoord");
    glVertexAttribPointer(gaTexCoordHandle, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), gTriangleVertices + 2);
    glEnableVertexAttribArray(gaTexCoordHandle);


    uTextureSamplerHandle = glGetUniformLocation(gProgram, "u_Sampler");

}

extern "C" JNIEXPORT void JNICALL
Java_com_example_king_openglenvdemo_nativegl_GLES2JNILib_step(
        JNIEnv* env,
        jobject obj,
        jobject jbitmap) {

    GLuint textureId = 0;

    glGenTextures(1, &textureId);
    if (textureId == 0) {
        LOGE("create textureId failed");
        return;
    }

    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, textureId);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

    AndroidBitmapInfo bitmapInfo;
    AndroidBitmap_getInfo(env, jbitmap, &bitmapInfo);

    LOGD("---------------bitmap width:%d, height:%d ", bitmapInfo.width, bitmapInfo.height);

    void * pixel;

    AndroidBitmap_lockPixels(env, jbitmap, &pixel);

    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bitmapInfo.width, bitmapInfo.height, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixel);
    glGenerateMipmap(GL_TEXTURE_2D);

    AndroidBitmap_unlockPixels(env, jbitmap);

    glUniform1i(uTextureSamplerHandle, 0);


    glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
}